package com.lzy.mall.product.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.product.entity.PmsBrand;
import com.lzy.mall.product.entity.PmsCategoryBrandRelation;
import com.lzy.mall.product.service.PmsCategoryBrandRelationService;
import com.lzy.mall.product.vo.BrandVo;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联(PmsCategoryBrandRelation)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:13
 */
@RestController
@RequestMapping("categorybrandrelation")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsCategoryBrandRelationController {
    private final PmsCategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    public ResultUtils cateLogList(@RequestParam("brandId") Long brandId){
        List<PmsCategoryBrandRelation> data = categoryBrandRelationService.cateLogList(brandId);
        return ResultUtils.ok().put("data",data);
    }

    /**
     *  /product/categorybrandrelation/brands/list
     *
     *  1、Controller：处理请求，接受和校验数据
     *  2、Service接受controller传来的数据，进行业务处理
     *  3、Controller接受Service处理完的数据，封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public ResultUtils relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){
        List<PmsBrand> brands = categoryBrandRelationService.getBrandsByCatId(catId);
        if (CollUtil.isEmpty(brands)){
            return ResultUtils.ok();
        }
        List<BrandVo> list = CollUtil.newArrayList();
        brands.forEach(a -> {
            BrandVo build = BrandVo.builder()
                    .brandId(a.getBrandId())
                    .brandName(a.getName())
                    .build();
            list.add(build);
        });
        return ResultUtils.ok().put("data",list);

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public ResultUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return ResultUtils.ok().put("page", page);
    }

    /**
     * 保存
     * 传入品牌ID brand_id和分类ID catelog_id
     */
    @PostMapping("/save")
    public ResultUtils save(@RequestBody PmsCategoryBrandRelation categoryBrandRelation){
        categoryBrandRelationService.save(categoryBrandRelation);
        return ResultUtils.ok();
    }

    /**
     * 查询单条数据
     */
    @GetMapping("/info/{id}")
    public ResultUtils getById(@PathVariable("id") Long id){
       PmsCategoryBrandRelation data = categoryBrandRelationService.getById(id);
       return ResultUtils.ok().put("categoryBrandRelation",data);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResultUtils update(@RequestBody PmsCategoryBrandRelation categoryBrandRelation){
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return ResultUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResultUtils delete(@RequestBody Long[] ids){
        categoryBrandRelationService.removeByIds(CollUtil.toList(ids));

        return ResultUtils.ok();
    }

}
